﻿using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Biotech.Utils
{
    public class jsonUtil
    {
        public static T DeserializeFile<T>(string filePath)
        {
            T result = default(T);
            string josnString = File.ReadAllText(filePath);
            if (File.Exists(filePath))
            {
                result = JsonConvert.DeserializeObject<T>(josnString);
            }
            return result;
        }

        /// <summary>
        /// 将对象生成Json文件
        /// </summary>
        /// <typeparam name="Json">文件路径</typeparam>
        /// <typeparam name="sourceObj">对象类</typeparam>
        public static void SerializerFile(string filePath, object sourceObj)
        {
            if (!string.IsNullOrWhiteSpace(filePath) && sourceObj != null)
            {
                string json = JsonConvert.SerializeObject(sourceObj);
                Console.WriteLine(json);
                File.WriteAllText(filePath, json);//将内容写进jon文件中  
            }
        }
    }
}
